<?php

class Asterye_waimaixia_plugin_oneModuleWxapp extends WeModuleWxapp {
    
	public function __construct() {

	}
	
	public function doPageOpenid() {
		global $_W,$_GPC;
		load()->func('communication'); 
		$userinfo2 = ihttp_get("https://api.weixin.qq.com/sns/jscode2session?appid={$_W['account']['key']}&secret={$_W['account']['secret']}&js_code={$_GPC['code']}&grant_type=authorization_code");
		if($userinfo2['status'] != 'OK') {			
			$this->result(1,'用户信息', '访问接口失败！');
		}
		$userinfo = json_decode($userinfo2['content'],true);
		$this->result(0,'用户信息', $userinfo);
    }

	public function doPageApi() {
		global $_W,$_GPC;
		$settings = $this->module['config'];
		$settings['waimaitt_meituansc_toppic'] = tomedia($settings['waimaitt_meituansc_toppic']);
		$settings['waimaitt_meituanwm_step1pic'] = tomedia($settings['waimaitt_meituanwm_step1pic']);
		$settings['waimaitt_meituanwm_step2pic'] = tomedia($settings['waimaitt_meituanwm_step2pic']);
		$settings['waimaitt_meituanwm_step3pic'] = tomedia($settings['waimaitt_meituanwm_step3pic']);
		$settings['waimaitt_meituanwm_step4pic'] = tomedia($settings['waimaitt_meituanwm_step4pic']);
		$settings['waimaitt_meituansc_step1pic'] = tomedia($settings['waimaitt_meituansc_step1pic']);
		$settings['waimaitt_meituansc_step2pic'] = tomedia($settings['waimaitt_meituansc_step2pic']);
		$settings['waimaitt_meituansc_step3pic'] = tomedia($settings['waimaitt_meituansc_step3pic']);
		$settings['waimaitt_meituansc_step4pic'] = tomedia($settings['waimaitt_meituansc_step4pic']);
		$settings['waimaitt_meituanwm_poster'] = tomedia($settings['waimaitt_meituanwm_poster']);
		$settings['waimaitt_meituansc_poster'] = tomedia($settings['waimaitt_meituansc_poster']);
		$settings['sharethumb'] = tomedia($settings['sharethumb']);
		$settings['feeds_thumb'] = tomedia($settings['feeds_thumb']);
		$settings['dianwo'] = tomedia($settings['dianwo']);
		$settings['waimai_guidepic'] = tomedia($settings['waimai_guidepic']);
		$settings['index_fullLoading_logoUrl'] = tomedia($settings['index_fullLoading_logoUrl']);
		$o = $_GPC["o"];
		$openid = trim($_GPC["openid"]);
		if(!$openid && $o != "setting"){
		    exit(json_encode(array('code' => 0, 'status' => 0, 'msg' => 'SUCCESS')));
		}
		switch ($o) {
			case "setting":
				unset($settings['xbapikey']);
				unset($settings['appkey']);
				unset($settings['apikey']);
				$backdata = array("setting" => $settings);
				$ret = json_encode($backdata);
				$data = array();
				$data["key"] = substr(md5(time() . uniqid()), 0, 16);
				$data["data"] = $this->aesEn($ret, $data["key"]);
				exit(json_encode(array('code' => 0, 'status' => 1, 'msg' => 'SUCCESS', 'data' => $data)));
		    case "userinfo":
				$invite_uid = intval($_GPC["invite_uid"]);
			    $nickname = trim($_GPC["nickname"]);
				$avatar = trim($_GPC["avatar"]);
			    if(!$invite_uid){$invite_uid = 0;}
				$user = pdo_get($this->modulename . "_user", array("openid" => $openid, "uniacid" => $_W["uniacid"]));
				$invite_user = pdo_get($this->modulename . "_user", array("user_id" => $invite_uid, "uniacid" => $_W["uniacid"]));
				if(!$invite_user){
					$parent_id = 0;
					$invite_uid = '0';
				}else{
					$parent_id = $invite_uid;
					$invite_uid = $invite_user['invite_uid'].'_'.$invite_uid;
				}
				if (empty($user)) {
					$user = array();
					$user["uniacid"] = $_W["uniacid"];
					$user["openid"] = $openid;
					$user["parent_id"] = $parent_id;
			        $user["invite_uid"] = $invite_uid;
					$user["nickname"] = $nickname;
					$user["avatar"] = $avatar;
					$user["create_time"] = $_W["timestamp"];
					pdo_insert($this->modulename . "_user", $user);
					$user_id = pdo_insertid();
					$user = pdo_get($this->modulename . "_user", array("user_id" => $user_id, "uniacid" => $_W["uniacid"]));
				}else{
					if($nickname && $avatar){
						pdo_update($this->modulename . "_user", array("nickname" => $nickname,"avatar" => $avatar), array("user_id" => $user["user_id"]));
						$user["nickname"] = $nickname;
					    $user["avatar"] = $avatar;
					}
					$user_id = $user['user_id'];
				}
				unset($settings['xbapikey']);
				unset($settings['appkey']);
				unset($settings['apikey']);
				$backdata = array("openid" => $openid, "userinfo" => $user, "setting" => $settings);
				$ret = json_encode($backdata);
				$data = array();
				$data["key"] = substr(md5(time() . uniqid()), 0, 16);
				$data["data"] = $this->aesEn($ret, $data["key"]);
				exit(json_encode(array('code' => 0, 'status' => 1, 'msg' => 'SUCCESS', 'data' => $data)));
			case "ad":
				$cateid = intval($_GPC["cateid"]);
				$condition = "uniacid = {$_W['uniacid']} AND cateid = {$cateid} AND is_show = 1";
				$orders = " ORDER BY displayorder DESC";
				$list = pdo_fetchall("SELECT * FROM ".tablename($this->modulename . "_ad")." WHERE ".$condition.$orders);
				$resArr['code'] = 0;
				$resArr['status'] = 1;
				$resArr['msg'] = 'SUCCESS';
				$resArr['swiperitems'] = array();
				foreach($list as $k=>$v){
					$resArr['swiperitems'][$k] = array('item_id'=>$v['id'],'title'=>$v['title'],'title_text'=>$v['title_text'],'title_desc'=>$v['title_desc'],'img'=>tomedia($v['thumb']),'adtype'=>$v['adtype'],'appid'=>$v['appid'],'path'=>$v['path'],'url'=>$v['url'],'opentype'=>'click','h5_url'=>$v['url'],'reply_thumb'=>$v['reply_thumb'],'reply_text'=>$v['reply_text'],'link_title' => $v['link_title'],'link_desc' => $v['link_desc'],'link_thumb' => $v['link_thumb'],'link_url' => $v['link_url'],'is_ios'=>$v['is_ios']);
				}
				$ret = array();
				$ret["data"] = $resArr;
				$ret = json_encode($ret);
				$data = array();
				$data["key"] = substr(md5(time() . uniqid()), 0, 16);
				$data["data"] = $this->aesEn($ret, $data["key"]);
				exit(json_encode(array('code' => 0, 'status' => 1, 'msg' => 'SUCCESS', 'data' => $data)));
			case "ad_heng":
				$condition = "uniacid = {$_W['uniacid']} AND cateid = 2 AND is_show = 1";
				$orders = " ORDER BY displayorder DESC";
				$list = pdo_fetchall("SELECT * FROM ".tablename($this->modulename . "_ad")." WHERE ".$condition.$orders);
				$resArr['code'] = 0;
				$resArr['status'] = 1;
				$resArr['msg'] = 'SUCCESS';
				$resArr['swiperitems'] = array();
				foreach($list as $k=>$v){
					$resArr['ad_heng'][$k] = array('item_id'=>$v['id'],'bgcolor'=>$v['bgcolor'],'title'=>$v['title'],'title_text'=>$v['title_text'],'title_desc'=>$v['title_desc'],'img'=>tomedia($v['thumb']),'adtype'=>$v['adtype'],'appid'=>$v['appid'],'path'=>$v['path'],'url'=>$v['url'],'opentype'=>'click','h5_url'=>$v['url'],'reply_thumb'=>$v['reply_thumb'],'reply_text'=>$v['reply_text'],'link_title' => $v['link_title'],'link_desc' => $v['link_desc'],'link_thumb' => $v['link_thumb'],'link_url' => $v['link_url'],'is_ios'=>$v['is_ios']);
				}
				$ret = array();
				$ret["data"] = $resArr;
				$ret = json_encode($ret);
				$data = array();
				$data["key"] = substr(md5(time() . uniqid()), 0, 16);
				$data["data"] = $this->aesEn($ret, $data["key"]);
				exit(json_encode(array('code' => 0, 'status' => 1, 'msg' => 'SUCCESS', 'data' => $data)));
			case "menu":
				$condition = "uniacid = {$_W['uniacid']} AND cateid >= 61 AND cateid <= 69 AND is_show = 1";
				$orders = " ORDER BY displayorder DESC";
				$list = pdo_fetchall("SELECT * FROM ".tablename($this->modulename . "_ad")." WHERE ".$condition.$orders);
				$resArr['code'] = 0;
				$resArr['status'] = 1;
				$resArr['msg'] = 'SUCCESS';
				$resArr['menu'] = array();
				foreach($list as $k=>$v){
					$resArr['menu'][$k] = array('item_id'=>$v['id'],'bgcolor'=>$v['bgcolor'],'title'=>$v['title'],'title_text'=>$v['title_text'],'title_desc'=>$v['title_desc'],'img'=>tomedia($v['thumb']),'adtype'=>$v['adtype'],'appid'=>$v['appid'],'path'=>$v['path'],'url'=>$v['url'],'opentype'=>'click','h5_url'=>$v['url'],'reply_thumb'=>$v['reply_thumb'],'reply_text'=>$v['reply_text'],'link_title' => $v['link_title'],'link_desc' => $v['link_desc'],'link_thumb' => $v['link_thumb'],'link_url' => $v['link_url'],'is_ios'=>$v['is_ios'],'waimaitt_list_bgcolor'=>$v['waimaitt_list_bgcolor'],'waimaitt_list_leftupicon'=>tomedia($v['waimaitt_list_leftupicon']),'waimaitt_list_leftdownicon'=>tomedia($v['waimaitt_list_leftdownicon']),'waimaitt_list_jine'=>$v['waimaitt_list_jine'],'waimaitt_list_jineunit'=>$v['waimaitt_list_jineunit'],'waimaitt_list_jinerighttext'=>$v['waimaitt_list_jinerighttext'],'waimaitt_list_lingqutext'=>$v['waimaitt_list_lingqutext'],'waimaitt_list_lingqucolor'=>$v['waimaitt_list_lingqucolor'],'waimaitt_list_lingqubgcolor'=>$v['waimaitt_list_lingqubgcolor']);
				}
				$ret = array();
				$ret["data"] = $resArr;
				$ret = json_encode($ret);
				$data = array();
				$data["key"] = substr(md5(time() . uniqid()), 0, 16);
				$data["data"] = $this->aesEn($ret, $data["key"]);
				exit(json_encode(array('code' => 0, 'status' => 1, 'msg' => 'SUCCESS', 'data' => $data)));
			case "waimaidaohang":
				$condition = "uniacid = {$_W['uniacid']} AND cateid = 11 AND is_show = 1";
				$orders = " ORDER BY displayorder DESC";
				$list = pdo_fetchall("SELECT * FROM ".tablename($this->modulename . "_ad")." WHERE ".$condition.$orders);
				$resArr['code'] = 0;
				$resArr['status'] = 1;
				$resArr['msg'] = 'SUCCESS';
				$resArr['swiperitems'] = array();
				foreach($list as $k=>$v){
					$resArr['waimaidaohang'][$k] = array('item_id'=>$v['id'],'bgcolor'=>$v['bgcolor'],'title'=>$v['title'],'title_text'=>$v['title_text'],'title_desc'=>$v['title_desc'],'img'=>tomedia($v['thumb']),'adtype'=>$v['adtype'],'appid'=>$v['appid'],'path'=>$v['path'],'url'=>$v['url'],'opentype'=>'click','h5_url'=>$v['url'],'reply_thumb'=>$v['reply_thumb'],'reply_text'=>$v['reply_text'],'link_title' => $v['link_title'],'link_desc' => $v['link_desc'],'link_thumb' => $v['link_thumb'],'link_url' => $v['link_url'],'is_ios'=>$v['is_ios']);
				}
				$ret = array();
				$ret["data"] = $resArr;
				$ret = json_encode($ret);
				$data = array();
				$data["key"] = substr(md5(time() . uniqid()), 0, 16);
				$data["data"] = $this->aesEn($ret, $data["key"]);
				exit(json_encode(array('code' => 0, 'status' => 1, 'msg' => 'SUCCESS', 'data' => $data)));
			case "waimailist":
				$condition = "uniacid = {$_W['uniacid']} AND cateid = 12 AND is_show = 1";
				$orders = " ORDER BY displayorder DESC";
				$list = pdo_fetchall("SELECT * FROM ".tablename($this->modulename . "_ad")." WHERE ".$condition.$orders);
				$resArr['code'] = 0;
				$resArr['status'] = 1;
				$resArr['msg'] = 'SUCCESS';
				$resArr['swiperitems'] = array();
				foreach($list as $k=>$v){
					$resArr['waimailist'][$k] = array('item_id'=>$v['id'],'bgcolor'=>$v['bgcolor'],'title'=>$v['title'],'title_text'=>$v['title_text'],'title_desc'=>$v['title_desc'],'img'=>tomedia($v['thumb']),'adtype'=>$v['adtype'],'appid'=>$v['appid'],'path'=>$v['path'],'url'=>$v['url'],'opentype'=>'click','h5_url'=>$v['url'],'reply_thumb'=>$v['reply_thumb'],'reply_text'=>$v['reply_text'],'link_title' => $v['link_title'],'link_desc' => $v['link_desc'],'link_thumb' => $v['link_thumb'],'link_url' => $v['link_url'],'is_ios'=>$v['is_ios']);
				}
				$ret = array();
				$ret["data"] = $resArr;
				$ret = json_encode($ret);
				$data = array();
				$data["key"] = substr(md5(time() . uniqid()), 0, 16);
				$data["data"] = $this->aesEn($ret, $data["key"]);
				exit(json_encode(array('code' => 0, 'status' => 1, 'msg' => 'SUCCESS', 'data' => $data)));
			case "waimaittdaohang":
				$condition = "uniacid = {$_W['uniacid']} AND cateid = 21 AND is_show = 1";
				$orders = " ORDER BY displayorder DESC";
				$list = pdo_fetchall("SELECT * FROM ".tablename($this->modulename . "_ad")." WHERE ".$condition.$orders);
				$resArr['code'] = 0;
				$resArr['status'] = 1;
				$resArr['msg'] = 'SUCCESS';
				$resArr['swiperitems'] = array();
				foreach($list as $k=>$v){
					$resArr['waimaidaohang'][$k] = array('item_id'=>$v['id'],'bgcolor'=>$v['bgcolor'],'title'=>$v['title'],'title_text'=>$v['title_text'],'title_desc'=>$v['title_desc'],'img'=>tomedia($v['thumb']),'adtype'=>$v['adtype'],'appid_type'=>$v['appid_type'],'appid'=>$v['appid'],'path'=>$v['path'],'url'=>$v['url'],'opentype'=>'click','h5_url'=>$v['url'],'reply_thumb'=>$v['reply_thumb'],'reply_text'=>$v['reply_text'],'link_title' => $v['link_title'],'link_desc' => $v['link_desc'],'link_thumb' => $v['link_thumb'],'link_url' => $v['link_url'],'is_ios'=>$v['is_ios']);
				}
				$ret = array();
				$ret["data"] = $resArr;
				$ret = json_encode($ret);
				$data = array();
				$data["key"] = substr(md5(time() . uniqid()), 0, 16);
				$data["data"] = $this->aesEn($ret, $data["key"]);
				exit(json_encode(array('code' => 0, 'status' => 1, 'msg' => 'SUCCESS', 'data' => $data)));
			case "waimaittlist":
				$condition = "uniacid = {$_W['uniacid']} AND cateid = 22 AND is_show = 1";
				$orders = " ORDER BY displayorder DESC";
				$list = pdo_fetchall("SELECT * FROM ".tablename($this->modulename . "_ad")." WHERE ".$condition.$orders);
				$resArr['code'] = 0;
				$resArr['status'] = 1;
				$resArr['msg'] = 'SUCCESS';
				$resArr['swiperitems'] = array();
				foreach($list as $k=>$v){
					$resArr['waimailist'][$k] = array('item_id'=>$v['id'],'bgcolor'=>$v['bgcolor'],'title'=>$v['title'],'title_text'=>$v['title_text'],'title_desc'=>$v['title_desc'],'img'=>tomedia($v['thumb']),'adtype'=>$v['adtype'],'appid_type'=>$v['appid_type'],'appid'=>$v['appid'],'path'=>$v['path'],'url'=>$v['url'],'opentype'=>'click','h5_url'=>$v['url'],'reply_thumb'=>$v['reply_thumb'],'reply_text'=>$v['reply_text'],'link_title' => $v['link_title'],'link_desc' => $v['link_desc'],'link_thumb' => $v['link_thumb'],'link_url' => $v['link_url'],'is_ios'=>$v['is_ios'],'waimaitt_list_bgcolor'=>$v['waimaitt_list_bgcolor'],'waimaitt_list_leftupicon'=>tomedia($v['waimaitt_list_leftupicon']),'waimaitt_list_leftdownicon'=>tomedia($v['waimaitt_list_leftdownicon']),'waimaitt_list_jine'=>$v['waimaitt_list_jine'],'waimaitt_list_jineunit'=>$v['waimaitt_list_jineunit'],'waimaitt_list_jinerighttext'=>$v['waimaitt_list_jinerighttext'],'waimaitt_list_lingqutext'=>$v['waimaitt_list_lingqutext'],'waimaitt_list_lingqucolor'=>$v['waimaitt_list_lingqucolor'],'waimaitt_list_lingqubgcolor'=>$v['waimaitt_list_lingqubgcolor']);
				}
				$ret = array();
				$ret["data"] = $resArr;
				$ret = json_encode($ret);
				$data = array();
				$data["key"] = substr(md5(time() . uniqid()), 0, 16);
				$data["data"] = $this->aesEn($ret, $data["key"]);
				exit(json_encode(array('code' => 0, 'status' => 1, 'msg' => 'SUCCESS', 'data' => $data)));
			case "cate":
				$resArr['mainCate'] = array();
				$resArr['products'] = array();
				$resArr['mainCate'][0] = array('maincateid'=>0,'cateid'=>0,'name'=>'热门推荐','hidden'=>0);
				//如果使用惠花首页热门商品
				if($settings['huihot'] == 1){
					$huihot = pdo_get('hccard_diy',array('only'=>'indexNav'.$settings['huiuniacid'],'uniacid'=>$settings['huiuniacid']));
					$huihot_items = json_decode($huihot['value'],true);
					foreach($huihot_items as $kk=>$vv){
						if($kk<=10){
							if(strpos($vv['link'],'http') !==false){
								$link = $vv['link'].'&pid='.$settings['huiuserid'];
							}else{
								$link = $settings['huidomain'].'/'.$vv['link'].'&pid='.$settings['huiuserid'];
							}
						    $resArr['products']['cate0products'][$kk] = array('cateid'=>$kk,'productId'=>'9999'.$vv['id'],'title'=>$vv['name'],'title_fu'=>$vv['desc'],'title_desc'=>'','title_text'=>'','thumb'=>tomedia($vv['thumb']),'huitype'=>3,'appid'=>$vv['appid'],'path'=>$vv['path'],'url'=>$vv['link'],'link_title'=>$vv['name'].'，'.$vv['desc'],'link_desc'=>'点击购买','link_thumb'=>tomedia($vv['thumb']),'link_url'=>$link,'is_ios'=>1,'hidden'=>0);
						}
					}
			    }else{
					$condition_hot = "uniacid = {$_W['uniacid']} AND enabled = 1 AND is_hot = 1";
					$orders_hot = " ORDER BY displayorder DESC";
					$list_hot = pdo_fetchall("SELECT * FROM ".tablename($this->modulename . "_tequan_item")." WHERE ".$condition_hot.$orders_hot);
					foreach($list_hot as $kk=>$vv){
						$resArr['products']['cate0products'][$kk] = array('cateid'=>$kk,'fa_uid'=>$fa_uid,'productId'=>$vv['item_id'],'title'=>$vv['title'],'title_fu'=>$vv['title_fu'],'title_desc'=>$vv['title_desc'],'title_text'=>$vv['title_text'],'thumb'=>tomedia($vv['thumb']),'huitype'=>$vv['huitype'],'appid'=>$vv['appid'],'path'=>$vv['path'],'url'=>$vv['url'],'reply_thumb'=>$vv['reply_thumb'],'reply_text'=>$vv['reply_text'],'is_ios'=>$vv['is_ios'],'hidden'=>0,'link_title' => $vv['link_title'],'link_desc' => $vv['link_desc'],'link_thumb' => tomedia($vv['link_thumb']),'link_url' => $vv['link_url']);
					}
				}
				
				//如果使用惠花首页特权商品
				if($settings['huicate'] == 1){
					$condition = "uniacid = {$settings['huiuniacid']} AND `order` > 0";
				    $orders = " ORDER BY `order` ASC";
					$huicate = pdo_fetchall("SELECT * FROM ".tablename("hccard_privilege_category")." WHERE ".$condition.$orders);
					foreach($huicate as $k=>$v){
						$resArr['mainCate'][$v['order']] = array('maincateid'=>$v['order'],'cateid'=>$v['order'],'name'=>$v['category_name'],'hidden'=>0);
						if($v['hcid'] == 'online'){
							$condition2 = "uniacid = {$settings['huiuniacid']} AND status = 1";
							$orders2 = " ORDER BY hcid ASC";
							$list2 = pdo_fetchall("SELECT * FROM ".tablename("hccard_kaquan_goods")." WHERE ".$condition2.$orders2);
							foreach($list2 as $kk=>$vv){
								if($settings['huicatehzs'] == 1){
									$link_url = $settings['huidomain'].'/app/index.php?i='.$settings['huiuniacid'].'&c=entry&hcid='.$vv['hcid'].'&act=kqdetail&do=officialpartner&m=hc_card&id='.$settings['huihzsid'];
								}else{
									$link_url = $settings['huidomain'].'/app/index.php?i='.$settings['huiuniacid'].'&c=entry&hcid='.$vv['hcid'].'&do=ticket&m=hc_card&pid='.$settings['huiuserid'];
								}
								$resArr['products']['cate'.$v['order'].'products'][$kk] = array('cateid'=>$kk,'productId'=>$vv['id'],'title'=>$vv['goods_name'],'title_fu'=>$vv['describe'],'title_desc'=>'','title_text'=>'','thumb'=>tomedia($vv['goods_logo']),'huitype'=>3,'appid'=>$vv['appid'],'path'=>$vv['path'],'url'=>$vv['url'],'link_title'=>$vv['goods_name'].'，'.$vv['describe'],'link_desc'=>'点击购买','link_thumb'=>tomedia($vv['goods_logo']),'link_url'=>$link_url,'is_ios'=>1,'hidden'=>0);
							}
						}else{
							$condition2 = "uniacid = {$settings['huiuniacid']} AND status = 0 AND sort = {$v['hcid']}";
							$orders2 = " ORDER BY id ASC";
							$list2 = pdo_fetchall("SELECT * FROM ".tablename("hccard_privilege")." WHERE ".$condition2.$orders2);
							foreach($list2 as $kk=>$vv){
								if($settings['huicatehzs'] == 1){
									if(strpos($vv['url'],'http') !== false){
										$link_url = $vv['url'];
									}elseif(strpos($vv['url'],'c=entry') !== false){
										$link_url = $settings['huidomain'].$vv['url'].'&id='.$settings['huiuniacid'];
									}else{
										$link_url = $settings['huidomain'].'/app/index.php?i='.$settings['huiuniacid'].'&c=entry&cate='.$vv['url'].'&act=detail&do=officialpartner&m=hc_card&id='.$settings['huihzsid'];
									}
								}else{
									if(strpos($vv['url'],'http') !== false){
										$link_url = $vv['url'];
									}elseif(strpos($vv['url'],'c=entry') !== false){
										$link_url = $settings['huidomain'].$vv['url'].'&id='.$settings['huiuniacid'];
									}else{
										$link_url = $settings['huidomain'].'/app/index.php?i='.$settings['huiuniacid'].'&c=entry&cate='.$vv['url'].'&do=recharge&m=hc_card&pid='.$settings['huiuserid'];
									}
									
								}
								$resArr['products']['cate'.$v['order'].'products'][$kk] = array('cateid'=>$kk,'productId'=>$vv['id'],'title'=>$vv['title1'],'title_fu'=>$vv['title2'],'title_desc'=>'','title_text'=>'','thumb'=>tomedia($vv['new_logo']),'huitype'=>3,'appid'=>$vv['appid'],'path'=>$vv['path'],'url'=>$vv['url'],'link_title'=>$vv['title1'].'，'.$vv['title2'],'link_desc'=>'点击购买','link_thumb'=>tomedia($vv['new_logo']),'link_url'=>$link_url,'is_ios'=>1,'hidden'=>0);
							}
						}
						
					}
				}else{
					$condition = "uniacid = {$_W['uniacid']} AND enabled = 1";
					$orders = " ORDER BY displayorder DESC";
					$list = pdo_fetchall("SELECT * FROM ".tablename($this->modulename . "_tequan_cate")." WHERE ".$condition.$orders);
					foreach($list as $k=>$v){
						$resArr['mainCate'][$k+1] = array('maincateid'=>$k+1,'cateid'=>$v['id'],'name'=>$v['catename'],'hidden'=>0);
						$condition2 = "uniacid = {$_W['uniacid']} AND enabled = 1 AND cateid = {$v['id']}";
						$orders2 = " ORDER BY displayorder DESC";
						$list2 = pdo_fetchall("SELECT * FROM ".tablename($this->modulename . "_tequan_item")." WHERE ".$condition2.$orders2);
						foreach($list2 as $kk=>$vv){
							$resArr['products']['cate'.$v['id'].'products'][$kk] = array('cateid'=>$kk,'productId'=>$vv['item_id'],'title'=>$vv['title'],'title_fu'=>$vv['title_fu'],'title_desc'=>$vv['title_desc'],'title_text'=>$vv['title_text'],'thumb'=>tomedia($vv['thumb']),'huitype'=>$vv['huitype'],'appid'=>$vv['appid'],'path'=>$vv['path'],'url'=>$vv['url'],'reply_thumb'=>$vv['reply_thumb'],'reply_text'=>$vv['reply_text'],'is_ios'=>$vv['is_ios'],'hidden'=>0,'link_title' => $vv['link_title'],'link_desc' => $vv['link_desc'],'link_thumb' => tomedia($vv['link_thumb']),'link_url' => $vv['link_url']);
						}
					}
				}

				$ret = array();
				$ret["data"] = $resArr;
				$ret = json_encode($ret);
				$data = array();
				$data["key"] = substr(md5(time() . uniqid()), 0, 16);
				$data["data"] = $this->aesEn($ret, $data["key"]);
				exit(json_encode(array('code' => 0, 'status' => 1, 'msg' => $huihot_items, 'data' => $data)));
			case "tequan_kefubind":
				$data['type'] = trim($_GPC["adtype"]);
			    $is_ios = intval($_GPC["is_ios"]);
			    $systemtype = trim($_GPC["systemtype"]);
				if($is_ios && $systemtype == 'ios'){
					$data['link_title'] = '由于相关规范，ios功能暂不可用';
					$data['link_desc'] = '暂不可用';
					$data['link_thumb'] = trim($_GPC["link_thumb"]);
					$data['link_url'] = '';
					$data['active_time'] = time();
				}else{
					$data['link_title'] = trim($_GPC["link_title"]);
					$data['link_desc'] = trim($_GPC["link_desc"]);
					$data['link_thumb'] = trim($_GPC["link_thumb"]);
					$data['link_url'] = trim($_GPC["link_url"]);
					$data['active_time'] = time();
				}
				
				$kefubind = pdo_get($this->modulename . "_kefubind", array("openid" => $openid));
				if(!$kefubind){
					$data['uniacid'] = $_W["uniacid"];
				    $data['openid'] = $openid;
					pdo_insert($this->modulename . "_kefubind", $data);
				}else{
					pdo_update($this->modulename . "_kefubind", $data, array('openid' => $openid));
				}
				exit(json_encode(array('code' => 0, 'status' => 1, 'msg' => 'SUCCESS')));
			case "ad_kefubind":
				$data['ad_id'] = intval($_GPC["adid"]);
				$data['type'] = trim($_GPC["adtype"]);
				$data['active_time'] = time();
				$kefubind = pdo_get($this->modulename . "_kefubind", array("openid" => $openid));
				if(!$kefubind){
					$data['uniacid'] = $_W["uniacid"];
				    $data['openid'] = $openid;
					pdo_insert($this->modulename . "_kefubind", $data);
				}else{
					pdo_update($this->modulename . "_kefubind", $data, array('openid' => $openid));
				}
				exit(json_encode(array('code' => 0, 'status' => 1, 'msg' => 'SUCCESS')));
			case "goods_kefubind":
				$data['ad_id'] = intval($_GPC["adid"]);
				$data['type'] = trim($_GPC["adtype"]);
				$data['active_time'] = time();
				$kefubind = pdo_get($this->modulename . "_kefubind", array("openid" => $openid));
				if(!$kefubind){
					$data['uniacid'] = $_W["uniacid"];
				    $data['openid'] = $openid;
					pdo_insert($this->modulename . "_kefubind", $data);
				}else{
					pdo_update($this->modulename . "_kefubind", $data, array('openid' => $openid));
				}
				
				exit(json_encode(array('code' => 0, 'status' => 'ok')));
			case 'goodsfeeds':
				$goods_id = intval($_GPC["goods_id"]);
				$keywords = trim($_GPC["keywords"]);
				$psize = 10;
				$pindex = max(1, intval($_GPC["page"]));
				$condition = " WHERE is_show = 1 ";
				if($keywords){
					$condition .= " AND content like '%".$keywords."%' ";
				}
				if($goods_id > 0){
					$condition .= " AND item_id = ".$goods_id;
				}
				if(!$settings['istao']){
					$condition .= " AND goods_pintai <> 'tb' ";
				}
				$orderby = " ORDER BY displayorder,create_time DESC ";
				$list = pdo_fetchall("SELECT * FROM " . tablename($this->modulename . "_goodsfeeds") . $condition . $orderby . " LIMIT " . ($pindex - 1) * $psize . "," . $psize);
				$total = pdo_fetchcolumn("SELECT COUNT(*) FROM " . tablename($this->modulename . "_goodsfeeds") . $condition);
				$feeds = array();
				foreach ($list as $key => $val) {
					$photos = array();
					if (!empty($val["photos"])) {
						$photos = json_decode($val["photos"], true);
						foreach ($photos as $k => $v) {
							$photos[$k] = tomedia($v);
						}
					}
					
					$feeds[] = array("feed_id" => $val["item_id"], "date" => date('Y-m-d H:i',$val["create_time"]), "content" => $val["content"], "photos" => $photos, "shopname" => $val['nickname'], "pintai" => $val['goods_pintai'], "shopavatar" => $val['avatar'],'title_text'=>$val['title_text'],'title_desc'=>$val['title_desc'],'adtype'=>$val['adtype'],'appid'=>$val['appid'],'path'=>$val['path'],'url'=>$val['url'],'reply_thumb'=>tomedia($val['reply_thumb']),'reply_text'=>$val['reply_text'],'link_title' => $val['link_title'],'link_desc' => $val['link_desc'],'link_thumb' => tomedia($val['link_thumb']),'link_url' => $val['link_url']);
				}
				$ret = array();
				$ret["feeds"] = $feeds;
				$ret["total"] = ceil($total / $psize);
				$ret = json_encode($ret);
				$data = array();
				$data["key"] = substr(md5(time() . uniqid()), 0, 16);
				$data["data"] = $this->aesEn($ret, $data["key"]);
				exit(json_encode(array('code' => 0, 'status' => 1, 'msg' => 'SUCCESS', 'data' => $data)));
			case "coupon_list":
				$lat = trim($_GPC["lat"]);
			    $lon = trim($_GPC["lon"]);
				if(!intval($_GPC["cityid"])){
					$city = json_decode(file_get_contents('https://apimobile.meituan.com/group/v1/city/latlng/'.$lat.','.$lon.'?tag=0'),true);
				    $cityId = $city['data']['dpCityId'];
					$district = $city['data']['district'];
				}else{
					$cityId = intval($_GPC["cityid"]);
					$district = '';
				}
				$page = intval($_GPC["page"]);
				$catename = trim($_GPC["catename"]);
				if($catename == '餐饮'){$cat0Ids = 226;}
				if($catename == '休闲娱乐'){$cat0Ids = 3;}
				if($catename == '丽人'){$cat0Ids = 2;}
				if($catename == '亲子'){$cat0Ids = 389;}
				$data = '{"utmSource":"'.$settings['utmSource'].'","utmMedium":"'.$settings['utmMedium'].'","platformId":2,"ctxInfo":{"queryCityId":"'.$cityId.'","cat0Ids":['.$cat0Ids.'],"cat1Ids":[],"queryModule":2},"deviceInfo":{"os":2},"userInfo":{"userId":"","lat":'.$lat.',"lng":'.$lon.'},"queryIndex":'.$page.',"cxMap":{"uuid":"","fingerprint":"","h5fingerprint":"","wechatFingerprint":"","app":-1,"platform":11,"userAgent":"'.$_SERVER['HTTP_USER_AGENT'].'"},"uuid":""}';
				$opts = array(
					'http'=>array(
					'method'=>"POST",
					'header'=>"Content-type: application/json\r\n".
					"Content-length:".strlen($data)."\r\n" .
					"Cookie: userlat=".$lat.";userlng=".$lon.";\r\n" .
					"\r\n",
					'content' => $data
				    )
				);
				$cxContext = stream_context_create($opts);
				$coupon_list = file_get_contents("https://cps.meituan.com/ajax/union/shop/recommend?utmSource=".$settings['utmSource'], false, $cxContext);
				
				$ret = array();
				if($coupon_list){
				    $ret["data"] = json_decode($coupon_list,true)['data']['shopInfos'];
					if($ret["data"]){$ret["status"] = 'ok';}else{if($page>1){$ret["status"] = 'nomore';}else{$ret["status"] = 'empty';}}
				}else{
					$ret["data"] = array();
					$ret["status"] = 'empty';
				}
				$ret['cityid'] = $cityId;
				$ret['district'] = $district;
				$ret = json_encode($ret);
				$data = array();
				$data["key"] = substr(md5(time() . uniqid()), 0, 16);
				$data["data"] = $this->aesEn($ret, $data["key"]);
				exit(json_encode(array('code' => 0, 'status' => 1, 'msg' => 'SUCCESS', 'data' => $data)));
			case "coupon_list_search":
				$lat = trim($_GPC["lat"]);
			    $lon = trim($_GPC["lon"]);
				if(!intval($_GPC["cityid"])){
					$city = json_decode(file_get_contents('https://apimobile.meituan.com/group/v1/city/latlng/'.$lat.','.$lon.'?tag=0'),true);
					$cityId = $city['data']['dpCityId'];
				}else{
					$cityId = intval($_GPC["cityid"]);
				}
				$keyword = trim($_GPC["keyword"]);
				if($keyword){
					$page = intval($_GPC["page"]);
					$data = '{"utmSource":"'.$settings['utmSource'].'","utmMedium":"'.$settings['utmMedium'].'","platformId":2,"ctxInfo":{"queryCityId":"'.$cityId.'","cat0Ids":[],"cat1Ids":[],"queryKeyWord":"'.$keyword.'","queryModule":3},"deviceInfo":{"os":2},"userInfo":{"userId":"","lat":'.$lat.',"lng":'.$lon.'},"queryIndex":'.$page.',"cxMap":{"uuid":"","fingerprint":"","h5fingerprint":"","wechatFingerprint":"","app":-1,"platform":11,"userAgent":"'.$_SERVER['HTTP_USER_AGENT'].'"},"uuid":""}';
					$opts = array(
						'http'=>array(
						'method'=>"POST",
						'header'=>"Content-type: application/json\r\n".
						"Content-length:".strlen($data)."\r\n" .
						"Cookie: userlat=".$lat.";userlng=".$lon.";\r\n" .
						"\r\n",
						'content' => $data
						)
					);
					$cxContext = stream_context_create($opts);
					$coupon_list = file_get_contents("https://cps.meituan.com/ajax/union/shop/recommend?utmSource=".$settings['utmSource'], false, $cxContext);
					
					$ret = array();
					if($coupon_list){
						$ret["data"] = json_decode($coupon_list,true)['data']['shopInfos'];
						if($ret["data"]){$ret["status"] = 'ok';}else{if($page>1){$ret["status"] = 'nomore';}else{$ret["status"] = 'empty';}}
					}else{
						$ret["data"] = array();
						$ret["status"] = 'empty';
					}
				}else{
					$ret["data"] = array();
					$ret["status"] = 'empty';
				}
				
				$ret['cityid'] = $cityId;
				$ret = json_encode($ret);
				$data = array();
				$data["key"] = substr(md5(time() . uniqid()), 0, 16);
				$data["data"] = $this->aesEn($ret, $data["key"]);
				exit(json_encode(array('code' => 0, 'status' => 1, 'msg' => 'SUCCESS', 'data' => $data)));
			case "goodsshare":
				$coupon = urldecode(trim($_GPC["coupon"]));
			    $shop_id = intval($_GPC["shop_id"]);
				$deal_id = intval($_GPC["deal_id"]);
				$is_conpon = pdo_get($this->modulename . "_sharegoods", array("shop_id" => $shop_id,"deal_id" => $deal_id));
				if($is_conpon){
					$res = pdo_update($this->modulename . "_sharegoods", array('coupon' => $coupon), array("shop_id" => $shop_id,"deal_id" => $deal_id));
				}else{
					$res = pdo_insert($this->modulename . "_sharegoods", array('coupon' => $coupon,"shop_id" => $shop_id,"deal_id" => $deal_id));
				}
				$ret['status'] = 'ok';
				$ret = json_encode($ret);
				$data = array();
				$data["key"] = substr(md5(time() . uniqid()), 0, 16);
				$data["data"] = $this->aesEn($ret, $data["key"]);
				exit(json_encode(array('code' => 0, 'status' => 1, 'msg' => 'SUCCESS', 'data' => $data)));
			case "goodsdetail":
			    $shop_id = intval($_GPC["shop_id"]);
				$deal_id = intval($_GPC["deal_id"]);
				$coupon = pdo_get($this->modulename . "_sharegoods", array("shop_id" => $shop_id,"deal_id" => $deal_id));
				if($coupon){
					$ret['coupon'] = str_replace('&quot;','"',$coupon['coupon']);;
				}else{
					$ret['coupon'] = array();
				}
				$ret['nowdate'] = date('Y-m-d',time());
				$ret = json_encode($ret);
				$data = array();
				$data["key"] = substr(md5(time() . uniqid()), 0, 16);
				$data["data"] = $this->aesEn($ret, $data["key"]);
				exit(json_encode(array('code' => 0, 'status' => 1, 'msg' => 'SUCCESS', 'data' => $data)));
			case "goods_url":
				$ad_id = intval($_GPC["adid"]);
			    $pintai = trim($_GPC["pintai"]);
				$goods_id = trim($_GPC["goods_id"]);
				$val = pdo_get($this->modulename . "_groupchat", array("id" => $ad_id));
				if($pintai == 'jd'){
					if($settings['jdpid']){$pid = $settings['jdpid'];}else{$pid = '1000100';}
					preg_match_all('#https://u\.jd\.com/.{7}#i',$val['content'],$arr);
					if($arr){
						foreach ($arr[0] as $key1 => $val1) {
							//转链
							$jd = json_decode(file_get_contents('http://api-gw.haojingke.com/index.php/v1/api/jd/getunionurl?apikey='.$settings['apikey'].'&goods_id='.urlencode($val1).'&positionid='.$pid.'&type=3&couponurl='),true);
							if($jd['status_code'] == 200){
								//替换链接
								$val['content'] = str_replace($val1,explode("?",$jd['data'])[0],$val['content']);
							}
							//转链
						}
						$data['ad_id'] = $ad_id;
						$data['type'] = 'jd_goods';
						$data['content'] = $val["content"];
						$data['active_time'] = time();
						$kefubind = pdo_get($this->modulename . "_kefubind", array("openid" => $openid));
						if(!$kefubind){
							$data['uniacid'] = $_W["uniacid"];
							$data['openid'] = $openid;
							pdo_insert($this->modulename . "_kefubind", $data);
						}else{
							pdo_update($this->modulename . "_kefubind", $data, array('openid' => $openid));
						}
						
					}
				}elseif($pintai == 'tb'){
					if($val['tkl']){
						$lastid3 = pdo_fetch("select * from ".tablename($this->modulename . '_groupchat')." where tkl <> '' ORDER BY id desc");
						if($lastid3){
							$kkkk = $lastid3['link_url'];
						}
						if($settings['tbqudaoid'] && $settings['tbqudaopid']){
							$tb = json_decode(file_get_contents('https://api.zhetaoke.com:10001/api/open_gaoyongzhuanlian_tkl.ashx?appkey='.$settings['appkey'].'&sid='.$settings['sid'].'&pid='.$settings['tbqudaopid'].'&	relation_id='.$settings['tbqudaoid'].'&signurl=5&tkl='.$val['tkl']),true);
						}else{
							$tb = json_decode(file_get_contents('https://api.zhetaoke.com:10001/api/open_gaoyongzhuanlian_tkl.ashx?appkey='.$settings['appkey'].'&sid='.$settings['sid'].'&pid='.$settings['tbpid'].'&signurl=5&tkl='.$val['tkl']),true);
						}
						
						if($tb['status'] == 200){
							if($settings['kurl']){
								$val["link_url"] = $settings['kurl'].$tb['content'][0]['tkl'];
							}else{
								$val["link_url"] = str_replace("{淘口令}",$tb['content'][0]['tkl'],$kkkk);
								$val["link_url"] = str_replace("{base64(主图)}",base64_encode($tb['content'][0]["pict_url"]),$val["link_url"]);
							}
							$tb = $tb['content'][0];
							$data['ad_id'] = $ad_id;
							$data['type'] = 'tb_goods';
							$data['content'] = $val["content"];
							$data['link_title'] = $val["goods_title"];
							$data['link_thumb'] = $val["goods_img"];
							$data['link_desc'] = '点击购买';
							$data['link_url'] = $val["link_url"];
							$data['active_time'] = time();
							$kefubind = pdo_get($this->modulename . "_kefubind", array("openid" => $openid));
							if(!$kefubind){
								$data['uniacid'] = $_W["uniacid"];
								$data['openid'] = $openid;
								pdo_insert($this->modulename . "_kefubind", $data);
							}else{
								pdo_update($this->modulename . "_kefubind", $data, array('openid' => $openid));
							}
						}
					}
				}
				exit(json_encode(array('code' => 0, 'status' => 'ok', 'adtype' => $val['adtype'], 'appid' => $val['appid'], 'path' => $val['path'])));
			case "feeds_url":
				$ad_id = intval($_GPC["adid"]);
			    $pintai = trim($_GPC["pintai"]);
				$val = pdo_get($this->modulename . "_goodsfeeds", array("item_id" => $ad_id));
				if($pintai == 'tb'){
					if($val['goods_id']){
						if($settings['tbqudaoid'] && $settings['tbqudaopid']){
							$tb = json_decode(file_get_contents('https://api.zhetaoke.com:10001/api/open_gaoyongzhuanlian.ashx?appkey='.$settings['appkey'].'&sid='.$settings['sid'].'&pid='.$settings['tbqudaopid'].'&	relation_id='.$settings['tbqudaoid'].'&signurl=5&num_iid='.$val['goods_id']),true);
						}else{
							$tb = json_decode(file_get_contents('https://api.zhetaoke.com:10001/api/open_gaoyongzhuanlian.ashx?appkey='.$settings['appkey'].'&sid='.$settings['sid'].'&pid='.$settings['tbpid'].'&signurl=5&num_iid='.$val['goods_id']),true);
						}
						
						if($tb['status'] == 200){
							if($settings['kurl']){
								$val["link_url"] = $settings['kurl'];
							}
							$tb = $tb['content'][0];
							$data['ad_id'] = $ad_id;
							$data['type'] = 'tb_goods';
							$data['content'] = $val["content"];
							$data['link_title'] = $val["goods_title"];
							$data['link_thumb'] = $val["goods_img"];
							$data['link_desc'] = '点击购买';
							$kzurl = pdo_fetchall("SELECT link_url FROM ".tablename($this->modulename . "_groupchat")." WHERE link_url <> '' AND tkl <> '' order by id desc limit 1");
							$data["link_url"] = str_replace("{淘口令}",$tb['tkl'],$kzurl[0]['link_url']);
							$data['link_url'] = $data["link_url"].$tb['tkl'];
							$data['active_time'] = time();
							$kefubind = pdo_get($this->modulename . "_kefubind", array("openid" => $openid));
							if(!$kefubind){
								$data['uniacid'] = $_W["uniacid"];
								$data['openid'] = $openid;
								pdo_insert($this->modulename . "_kefubind", $data);
							}else{
								pdo_update($this->modulename . "_kefubind", $data, array('openid' => $openid));
							}
						}
					}
				}else{
					if($pintai == 'jd'){
						if($settings['jdpid']){$pid = $settings['jdpid'];}else{$pid = '1000100';}
						$jd = json_decode(file_get_contents('http://api-gw.haojingke.com/index.php/v1/api/jd/getunionurl?apikey='.$settings['apikey'].'&goods_id='.$val['goods_id'].'&positionid='.$pid.'&type=1'),true);
						$val['adtype'] = 1;
						$val['path'] = '/pages/union/proxy/proxy?spreadUrl='.urlencode($jd['data']);
						$val['appid'] = 'wxca1fe42a16552094';
					}elseif($pintai == 'pdd'){
						if($settings['pddpid']){$pid = $settings['pddpid'];}else{$pid = '1000100';}
						$pdd = json_decode(file_get_contents('http://api-gw.haojingke.com/index.php/v1/api/pdd/getunionurl?apikey='.$settings['apikey'].'&goods_id='.$val['goods_id'].'&positionid='.$pid.'&type=1'),true);
						$val['adtype'] = 1;
						$val['path'] = $pdd['data']['alldata']['we_app_info']['page_path'];
						$val['appid'] = 'wx32540bd863b27570';
					}elseif($pintai == 'vip'){
						if($settings['vippid']){$pid = $settings['vippid'];}else{$pid = '1000100';}
						$vip = json_decode(file_get_contents('http://api-gw.haojingke.com/index.php/v1/api/vip/getunionurl?apikey='.$settings['apikey'].'&chanTag='.$pid.'&goods_id='.$val['goods_id']),true);
						$val['adtype'] = 1;
						$val['path'] = $vip['data']['urlInfoList'][0]['vipWxUrl'];
						$val['appid'] = 'wxe9714e742209d35f';
					}
				
				}
				exit(json_encode(array('code' => 0, 'status' => 'ok', 'adtype' => $val['adtype'], 'appid' => $val['appid'], 'path' => $val['path'])));
			case "goods_cate":
				$pt = trim($_GPC["pintai"]);
				$condition = "uniacid = ".$_W['uniacid']." AND pintai = '".$pt."' AND is_show = 1";
				$orders = " ORDER BY displayorder DESC";
				$list = pdo_fetchall("SELECT catename FROM ".tablename($this->modulename . "_goods_cate")." WHERE ".$condition.$orders);
				$resArr['code'] = 0;
				$resArr['status'] = 1;
				$resArr['msg'] = 'SUCCESS';
				$resArr['cate'] = array('全部');
				foreach ($list as $key => $val) {
					array_push($resArr['cate'],$val['catename']);
				}
				$ret = array();
				$ret["data"] = $resArr;
				$ret = json_encode($ret);
				$data = array();
				$data["key"] = substr(md5(time() . uniqid()), 0, 16);
				$data["data"] = $this->aesEn($ret, $data["key"]);
				exit(json_encode(array('code' => 0, 'status' => 1, 'msg' => 'SUCCESS', 'data' => $data)));
			case 'groupchats':
				$lastmsgid = intval($_GPC["lastid"]);
				$catename = trim($_GPC["catename"]);
				if($catename == 'undefined'){$catename = '全部';}
				$groupid = intval($_GPC["groupid"]);
				$useropenid = trim($_GPC["useropenid"]);
				if(!$useropenid){$g=1;}else{$g=$useropenid;}
				$keywords = trim($_GPC["keywords"]);
				$psize = 20;
				$pindex = 1;
				$condition = " WHERE groupid = {$groupid}";
				if($catename && $catename != '全部'){
					$cate = pdo_get($this->modulename . "_goods_cate", array("pintai" => $useropenid,"catename" => $catename,'is_show' => 1));
					if($cate){
						$condition .= " AND goods_cateid = {$cate['cateid']} ";
					}else{
						$condition .= " AND goods_cateid = 999999999999 ";
					}
				}
				if($lastmsgid > 0){
					$condition .= " AND id < {$lastmsgid} ";
				}
				if($keywords){
					$condition .= " AND content like '%".$keywords."%' ";
				}
				if($useropenid){
					$condition .= " AND openid = '".$useropenid."' ";
				}
				if(!$settings['istao']){
					$condition .= " AND goods_pintai <> 'tb' ";
				}
				$orderby = " ORDER BY time DESC ";
				$list = pdo_fetchall("SELECT * FROM ".tablename($this->modulename . "_groupchat") . $condition . $orderby . " LIMIT " . ($pindex - 1) * $psize . "," . $psize);
				if($list){$c = count($list);$lastmsgid = $list[$c-1]['id'];}else{$lastmsgid = 0;}
				$total = pdo_fetchcolumn("SELECT COUNT(*) FROM ".tablename($this->modulename . "_groupchat") . $condition);
				$msgs = array();
				foreach ($list as $key => $val) {
					if($val["openid"] == 'jd'){
						$val["nickname"] = '京东优惠线报';
					}
					if($val["openid"] == 'pdd'){
						$val["nickname"] = '拼多多优惠线报';
					}
					if($val["openid"] == 'vip'){
						$val["nickname"] = '唯品会优惠线报';
					}
					if($val["openid"] == 'tb'){
						$val["nickname"] = '淘猫优惠线报';
					}
					if(!$val["title"]){
						$msgtitle = '购买商品';
					}
					if($settings['huichat'] == 1){
						$val['adtype'] = 3;
					}
					if($val['adtype'] == 1){
						$val['adtype'] = 3;
					}
					if($catename == '全部'){
						$goods_cate = pdo_get($this->modulename . "_goods_cate", array("pintai" => $val["openid"],"cateid" => $val["goods_cateid"],'is_show' => 1));
						if($goods_cate){
							$msgs[] = array("id" => $val["id"],"group" => $g,"openid" => $val["openid"],"pintai" => $val["goods_pintai"],"goods_id" => $val["goods_id"],"name" => $val["nickname"],"face" => '../../static/images/'.$val["openid"].'.png', "date" => date('Y-m-d H:i:s',$val["time"]), "msg" => $val["content"], "goods_img" => $val["goods_img"], "ctype" => $val["type"],'title'=>$msgtitle,'title_text'=>$val['title_text'],'title_desc'=>$val['title_desc'],'adtype'=>$val['adtype'],'appid'=>$val['appid'],'path'=>$val['path'],'url'=>$val['url'],'link_title' => $val['link_title'],'link_desc' => $val['link_desc'],'link_thumb' => $val['link_thumb'],'link_url' => $val['link_url']);
						}
					}else{
						$msgs[] = array("id" => $val["id"],"group" => $g,"openid" => $val["openid"],"pintai" => $val["goods_pintai"],"goods_id" => $val["goods_id"],"name" => $val["nickname"],"face" => '../../static/images/'.$val["openid"].'.png', "date" => date('Y-m-d H:i:s',$val["time"]), "msg" => $val["content"], "goods_img" => $val["goods_img"], "ctype" => $val["type"],'title'=>$msgtitle,'title_text'=>$val['title_text'],'title_desc'=>$val['title_desc'],'adtype'=>$val['adtype'],'appid'=>$val['appid'],'path'=>$val['path'],'url'=>$val['url'],'link_title' => $val['link_title'],'link_desc' => $val['link_desc'],'link_thumb' => $val['link_thumb'],'link_url' => $val['link_url']);
					}
					
				}
				$ret = array();
				$ret["msgs"] = $msgs;
				if(count($msgs)==0){$status = 0;}else{$status = 1;}
				$ret["status"] = $status;
				$ret["lastid"] = $lastmsgid;
				$ret = json_encode($ret);
				$data = array();
				$data["key"] = substr(md5(time() . uniqid()), 0, 16);
				$data["data"] = $this->aesEn($ret, $data["key"]);
				
				exit(json_encode(array('code' => 0, 'status' => 'ok', 'msg' => $condition, 'data' => $data)));
		}
    }

	

	public function curlOpen($url, $config = array())
    {
		$arr = array('post' => false,'referer' => $url,'cookie' => '', 'useragent' => 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0; SLCC1; .NET CLR 2.0.50727; .NET CLR 3.0.04506; customie8)', 'timeout' => 20, 'return' => true, 'proxy' => '', 'userpwd' => '', 'nobody' => false,'header'=>array(),'gzip'=>true,'ssl'=>false,'isupfile'=>false);
		$arr = array_merge($arr, $config);
		
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, $arr['return']);
		curl_setopt($ch, CURLOPT_NOBODY, $arr['nobody']);  
		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
		curl_setopt($ch, CURLOPT_USERAGENT, $arr['useragent']);
		curl_setopt($ch, CURLOPT_REFERER, $arr['referer']);
		curl_setopt($ch, CURLOPT_TIMEOUT, $arr['timeout']);
		curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
		//curl_setopt($ch, CURLOPT_HEADER, true);//获取header
		if($arr['gzip']) curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate');
		
		if($arr['ssl'])
		{
			curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
			curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
		}
		if(!empty($arr['cookie']))
		{
			curl_setopt($ch, CURLOPT_COOKIEJAR, $arr['cookie']);
			curl_setopt($ch, CURLOPT_COOKIEFILE, $arr['cookie']); 
		} 
		
		if(!empty($arr['proxy']))
		{
			//curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);  
			curl_setopt ($ch, CURLOPT_PROXY, $arr['proxy']);
			if(!empty($arr['userpwd']))
			{            
				curl_setopt($ch,CURLOPT_PROXYUSERPWD,$arr['userpwd']);
			}        
		}    
		
		//ip比较特殊，用键值表示
		if(!empty($arr['header']['ip']))
		{
			array_push($arr['header'],'X-FORWARDED-FOR:'.$arr['header']['ip'],'CLIENT-IP:'.$arr['header']['ip']);
			unset($arr['header']['ip']);
		}   
		$arr['header'] = array_filter($arr['header']);
		
		if(!empty($arr['header']))
		{
			curl_setopt($ch, CURLOPT_HTTPHEADER, $arr['header']); 
		}

		if ($arr['post'] != false)
		{
			curl_setopt($ch, CURLOPT_POST, true);
			if(is_array($arr['post']) && $arr['isupfile'] === false)
			{
				$post = http_build_query($arr['post']);            
			} 
			else
			{
				$post = $arr['post'];
			}
			curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
		}
		$result = curl_exec($ch);
		curl_close($ch);

		return $result;
    }


	
	public function getdistance($lat1, $lng1, $lat2, $lng2, $len_type = 2, $decimal = 0) {
		$radLat1 = $lat1 * 3.1415926535898 / 180.0;
		$radLat2 = $lat2 * 3.1415926535898 / 180.0;
		$a = $radLat1 - $radLat2;
		$b = ($lng1 * 3.1415926535898 / 180.0) - ($lng2 * 3.1415926535898 / 180.0);
		$s = 2 * asin(sqrt(pow(sin($a/2),2) + cos($radLat1) * cos($radLat2) * pow(sin($b/2),2)));
		$s = $s * 6378.137;
		$s = round($s * 1000);
		if ($len_type > 1)
		{
		$s /= 1000;
		}
		$ddddecimal = round($s, $decimal);
		$ddddecimal = abs($ddddecimal);
		return $ddddecimal.'km';
	}

	public function aesEn($data, $key)
	{
		$data = base64_encode(openssl_encrypt($data, "AES-128-CBC", $key, OPENSSL_RAW_DATA, $key));
		return $data;
	}
	public function aesDe($data, $key)
	{
		echo $data;
		$data = base64_decode($data);
		$str = openssl_decrypt($data, "AES-128-CBC", $key, OPENSSL_RAW_DATA, $key);
		return $str;
	}
	
    
}
